const config = require('./../../config')
const uuidv1 = require('uuid/v1')
const passport = require('./../utils/passport')
const User_col = require('./../models/user')
const Passport_col = require('./..//models/password')

const get = async (ctx, next) => {
  ctx.status = 200
  ctx.body = {
    msg: 'get request',
    data: {
      data: ctx.request.query
    }
  }
}
const post = async (ctx, next) => {
  ctx.status = 200
  ctx.body = {
    msg: 'post request',
    data: {
      data: ctx.request.body
    }
  }
}

const login = async (ctx, next) => {
  const req = ctx.request.body
  const user = await User_col.findOne(
    {
      account: req.account
    },
    {
      _v: 0,
      id: 0
    }
  )
  if (!user) {
    ctx.status = 200
    ctx.body = {
      code: 0,
      msg: 'Account or Password error!'
    }
    return
  }
  const userId = user.userId
  const pass = await Passport_col.findOne(
    {
      userId
    },
    {
      hash: 1
    }
  )
  const match = await passport.validate(req.password, pass.hash)
  ctx.status = 200
  if (match) {
    ctx.body = {
      code: 1,
      msg: 'login success',
      data: user
    }
    return
  }
  ctx.body = {
    code: 0,
    msg: 'account or password error!'
  }
}
const register = async (ctx, next) => {
  const req = ctx.request.body
  const user = await User_col.findOne({
    account: req.account
  })
  ctx.status = 200
  if (user) {
    ctx.body = {
      code: 0,
      msg: '用户名重复'
    }
    return
  }
  const userId = uuidv1()
  const newUser = await User_col.create({
    userId,
    account: req.account
  })
  if (newUser) {
    const hash = await passport.encrypt(req.password, config.saltTime)
    const result = await Passport_col.create({
      userId,
      hash
    })
    if (result) {
      ctx.body = {
        code: 1,
        msg: '注册成功',
        data: {
          userId: newUser.userId,
          account: newUser.account
        }
      }
    }
  } else {
    ctx.body = {
      code: 0,
      msg: '注册失败！'
    }
  }
}

const updateUserInfo = async (ctx, next) => {
  const req = ctx.request.body
  const result = await User_col.update(
    {
      userId: req.userId
    },
    req
  )
  ctx.status = 200
  if (result.nModified === 1) {
    ctx.body = {
      code: 1,
      msg: 'save successed!'
    }
  } else {
    ctx.body = {
      code: 0,
      msg: 'save failed!'
    }
  }
}
module.exports = {
  get,
  post,
  login,
  register,
  updateUserInfo
}
